﻿@page "/SysTask"
@using SelfService.ViewModel.SysTaskVMs
@inherits BasePage
@attribute [ActionDescription("任务管理", "SelfService.Controllers,SysTask")]

<WTSearchPanel OnSearch="@DoSearch">
    <ValidateForm Model="@SearchModel">
        <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Inline">

        </Row>
    </ValidateForm>
</WTSearchPanel>

<Table @ref="dataTable"
       TItem="SysTaskView" 
       OnQueryAsync="OnSearch"
       IsPagination="true" 
       IsStriped="true" 
       IsBordered="true" 
       ShowRefresh="false"
       ShowToolbar="true"
       IsMultipleSelect="true" 
       ShowExtendButtons="true"
       ShowExtendDeleteButton="false"
       ShowExtendEditButton="false"
       ShowDefaultButtons="false" style="margin-top:10px;">
    <TableColumns>
        <TableColumn @bind-Field="@context.Name"  />
        <TableColumn @bind-Field="@context.ClassName"  />
        <TableColumn @bind-Field="@context.TaskType"  />
        <TableColumn @bind-Field="@context.LastRunTime" FormatString="yyyy-MM-dd HH: mm: ss" />
        <TableColumn @bind-Field="@context.LastEndTime" FormatString="yyyy-MM-dd HH: mm: ss" />
        <TableColumn @bind-Field="@context.NextRunTime" FormatString="yyyy-MM-dd HH: mm: ss" />
        <TableColumn @bind-Field="@context.TimeType"  />
        <TableColumn @bind-Field="@context.LastRunResult"  />
        <TableColumn @bind-Field="@context.CreateTime" FormatString="yyyy-MM-dd HH: mm: ss" />
        <TableColumn @bind-Field="@context.IsEnable" ComponentType="@typeof(Switch)" />
    </TableColumns>
    <TableToolbarTemplate>
        @if (IsAccessable("/api/SysTask/Add"))
        {
            <TableToolbarButton TItem="SysTaskView" Color="Color.Primary" Icon="fa fa-fw fa-plus" Text="@WtmBlazor.Localizer["Sys.Create"]" OnClickCallback="OnCreateClick" />
        }
        @if (IsAccessable("/api/SysTask/BatchDelete"))
        {
            <TableToolbarPopconfirmButton
                TItem="SysTaskView" Color="Color.Primary"
                Icon="fa fa-fw fa-trash-o" Text="@WtmBlazor.Localizer["Sys.BatchDelete"]"
                                          OnConfirm="@OnBatchDeleteClick" Content="@WtmBlazor.Localizer["Sys.BatchDeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]"
                                          ConfirmButtonText="@WtmBlazor.Localizer["Sys.BatchDelete"]" ConfirmButtonColor="Color.Danger" />
        }
        
    </TableToolbarTemplate>
    <RowButtonTemplate>
        <div style="padding-right:10px;">
            @if (IsAccessable("/api/SysTask/Edit"))
            {
                <TableCellButton Size="Size.ExtraSmall" Color="Color.Success" Icon="fa fa-edit" Text="@WtmBlazor.Localizer["Sys.Edit"]" OnClick="() => OnEditClick(context)" />
            }
            @if (IsAccessable("/api/SysTask/BatchDelete"))
            {
                <PopConfirmButton 
                    Icon="fa fa-trash-o" 
                    Text="@WtmBlazor.Localizer["Sys.Delete"]" 
                    OnConfirm="() => OnDeleteClick(context)"
                    Color="Color.Danger" 
                    Size="Size.ExtraSmall"
                    Content="@WtmBlazor.Localizer["Sys.DeleteConfirm"]" 
                    CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" 
                    ConfirmButtonText="@WtmBlazor.Localizer["Sys.Delete"]" 
                    ConfirmButtonColor="Color.Danger" />
            }
        </div>
    </RowButtonTemplate>
</Table>

@code{

    private SysTaskSearcher SearchModel = new SysTaskSearcher();
    private Table<SysTaskView> dataTable;


    protected override async Task OnInitializedAsync()
    {

        await base.OnInitializedAsync();
    }

    private async Task<QueryData<SysTaskView>> OnSearch(QueryPageOptions opts)
    {
        return await StartSearch<SysTaskView>("/api/SysTask/Search", SearchModel, opts);
    }

    private void DoSearch()
    {
        dataTable.QueryAsync();
    }

    private async Task OnCreateClick(IEnumerable<SysTaskView> items)
    {
        if (await OpenDialog<Create>(WtmBlazor.Localizer["Sys.Create"]) == DialogResult.Yes)
        {
            await dataTable.QueryAsync();
        }
    }

    private async Task OnEditClick(SysTaskView item)
    {
        if (await OpenDialog<Edit>(WtmBlazor.Localizer["Sys.Edit"], x => x.id == item.ID.ToString()) == DialogResult.Yes)
        {
            await dataTable.QueryAsync();
        }
    }
    
    private async Task OnBatchDeleteClick()
    {
        if (dataTable.SelectedRows?.Any() == true)
        {
            await PostsData(dataTable.SelectedRows.Select(x => x.ID).ToList(), $"/api/SysTask/batchdelete", (s) => WtmBlazor.Localizer["Sys.BatchDeleteSuccess", s]);
            await dataTable.QueryAsync();
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnDeleteClick(SysTaskView item)
    {
        await PostsData(new List<string> { item.ID.ToString() }, $"/api/SysTask/batchdelete", (s) => "Sys.OprationSuccess");
        await dataTable.QueryAsync();
    }




}
